const {
    stringValidator: {
        isInteger,
        isIpv4,
        isPort,
        isBlank,
    },
    CombinedPredicate: {
        not,
        and,
    },
    compose,
    toString,
} = rx
const validate = (() => {
    const additionalEmpty = (name, msg, checkFn) => RxElemValidator.create(name, msg, and(not(compose(toString, isBlank)), checkFn))
    return RxFormValidator.generate([
        additionalEmpty('backupIcpDay', '[自动备份天数] 只能为整数', not(isInteger)),
        additionalEmpty('backupIcpDay', '[自动备份天数] 必须大于 0', i => i < 0),
        additionalEmpty('backupIcpNum', '[保留备份数量] 只能为整数', not(isInteger)),
        additionalEmpty('backupIcpNum', '[保留备份数量] 必须大于 0', i => i < 0),

        additionalEmpty('backupFtpHost', '[主机名] 格式错误。示例 139.24.1.5', not(isIpv4)),
        additionalEmpty('backupFtpPort', '[端口] 格式错误。示例 8080', not(isPort)),
    ])
})()
const app = (() => {
    const dataBackupSettingConfigKey = 'data_backup_setting'
    const offSiteBackupSettingConfigKey = 'off_site_backup_setting'
    return new BasicTableVue({
        el: '#app',
        data() {
            return {
                api: sysBackupLogApi,
                columns: [
                    new TableColumn({prop: 'backupFile', title: '备份文件名称'}),
                    new TableColumn({prop: 'backupTime', title: '创建备份时间', formatter: TableColumn.datetimeFormat}),
                    new TableColumn({prop: 'operating', title: '操作', customComponent: true}),
                ],
                dataBackupSetting: new DataBackupSetting(),
                offSiteBackupSetting: new OffSiteBackupSetting(),
                sysBackupLogList: [],
            }
        },
        methods: {
            createForm() {
                return {}
            },
            async updateSettings() {
                if (!await validate({...this.dataBackupSetting, ...this.offSiteBackupSetting})) {
                    return
                }
                await ajaxOption({
                    res: sysBackupLogApi.updateSettings(this.dataBackupSetting, this.offSiteBackupSetting),
                    danger: '更新自动备份配置失败',
                    success: '更新自动备份配置成功',
                })
            },
            async initSettings() {
                if (!await validate({...this.dataBackupSetting, ...this.offSiteBackupSetting})) {
                    return
                }
                await ajaxOption({
                    res: sysBackupLogApi.initSettings(),
                    danger: '初始化备份配置失败',
                    success: '初始化备份配置成功',
                })
            },
            async getSettings() {
                const backupSettingMap = await sysBackupLogApi.getSettings()

                function getSetting(key) {
                    return JSON.parse(backupSettingMap[key].configValue)
                }

                this.dataBackupSetting = getSetting(dataBackupSettingConfigKey)
                this.offSiteBackupSetting = getSetting(offSiteBackupSettingConfigKey)
            },
            async manualBackup() {
                const res = await rxPrompt.confirm("确认是否要备份数据吗？", ['确认', '取消']);
                if (!res) {
                    return;
                }
                const settings = {...this.dataBackupSetting, ...this.offSiteBackupSetting}
                if (!await validate(settings)) {
                    return
                }
                if (await ajaxOption({
                    res: sysBackupLogApi.manualBackup(settings),
                    danger: '手动备份失败',
                    success: '手动备份成功',
                })) {
                    await this.searchPage()
                }
            },
            async deleteBackup(id) {
                if (await ajaxOption({
                    res: sysBackupLogApi.deleteBackup(id),
                    danger: '删除备份失败',
                    success: '删除备份成功',
                })) {
                    await this.searchPage()
                }
            },
            async restore(id) {
                const res = await rxPrompt.confirm("确认是否要恢复备份数据吗？", ['确认', '取消']);
                if (!res) {
                    return;
                }
                await ajaxOption({
                    res: sysBackupLogApi.restore(id),
                    danger: '恢复备份失败',
                    success: '恢复备份成功',
                })
            },
            async init() {
                await this.getSettings()
            },
        },
        computed: {
            fieldFormatter() {
                return fieldFormatter
            },
        },
    })
})()